package zzs.generalframework.console.core.authorization.authrole.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.authorization.authrole.AuthRoleBO;
import zzs.generalframework.common.core.authorization.authrole.AuthRoleInfo;
import zzs.generalframework.common.core.authorization.authrole.AuthRolePO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.authorization.authrole.dao.IAuthRoleDao;
import zzs.generalframework.console.core.authorization.authrole.service.IAuthRoleService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authrole.service.impl
 * @author zzs
 * @date 2018年2月1日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.authorization.authrole.service.impl.authRoleServiceImpl")
public class AuthRoleServiceImpl implements IAuthRoleService
{

	private Logger logger = LoggerFactory.getLogger(AuthRoleServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IAuthRoleDao authRoleDao;

	@Override
	public ConsoleReturnBO saveAuthRole(AuthRoleBO authRoleBO)
	{
		logger.debug("====保存权限角色信息开始====");
		logger.debug("====保存权限角色信息.入参:AuthRoleBO=[{}]====", authRoleBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthRolePO authRolePO = new AuthRolePO();
		copyAuthRoleBO2AuthRolePO(authRoleBO, authRolePO);
		Boolean insertResult = authRoleDao.insert(authRolePO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存权限角色信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限角色信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveAuthRole(List<AuthRoleBO> authRoleBOList) throws ConsoleException
	{
		logger.info("====保存权限角色信息列表开始====");
		logger.debug("====保存权限角色信息列表.入参:AuthRoleBOList=[{}]====", authRoleBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthRoleBO itemBO : authRoleBOList)
		{
			ConsoleReturnBO itemResult = saveAuthRole(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存权限角色信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限角色信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getAuthRole(AuthRoleInfo authRoleInfo)
	{
		logger.info("====获取权限角色信息开始====");
		logger.debug("====获取权限角色信息.入参:AuthRoleInfo=[{}]====", authRoleInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthRolePO> authRolePOList = authRoleDao.select(authRoleInfo);
		if (authRolePOList == null || authRolePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (authRolePOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			AuthRoleBO authRoleBO = new AuthRoleBO();
			copyAuthRolePO2AuthRoleBO(authRolePOList.get(0), authRoleBO);
			returnConsoleReturnBO.setReturnObj(authRoleBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			AuthRoleBO authRoleBO = new AuthRoleBO();
			copyAuthRolePO2AuthRoleBO(authRolePOList.get(0), authRoleBO);
			returnConsoleReturnBO.setReturnObj(authRoleBO);
		}
		logger.debug("====获取权限角色信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限角色信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listAuthRole(AuthRoleInfo authRoleInfo)
	{
		logger.info("====获取权限角色信息列表开始====");
		logger.debug("====获取权限角色信息列表.入参:AuthRoleInfo=[{}]====", authRoleInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthRolePO> authRolePOList = authRoleDao.select(authRoleInfo);
		if (authRolePOList == null || authRolePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<AuthRoleBO> resultList = new ArrayList<AuthRoleBO>();
			for (AuthRolePO itemPO : authRolePOList)
			{
				AuthRoleBO itemBO = new AuthRoleBO();
				copyAuthRolePO2AuthRoleBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取权限角色信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限角色信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthRole(AuthRoleBO authRoleBO)
	{
		logger.info("====更新权限角色信息开始====");
		logger.debug("====更新权限角色信息.入参:AuthRoleBO=[{}]====", authRoleBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthRolePO authRolePO = new AuthRolePO();
		copyAuthRoleBO2AuthRolePO(authRoleBO, authRolePO);
		Boolean updateResult = authRoleDao.update(authRolePO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新权限角色信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限角色信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthRole(List<AuthRoleBO> authRoleBOList) throws ConsoleException
	{
		logger.info("====更新权限角色信息列表开始====");
		logger.debug("====更新权限角色信息列表.入参:List<AuthRoleBO>=[{}]====", authRoleBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthRoleBO itemBO : authRoleBOList)
		{
			ConsoleReturnBO itemResult = updateAuthRole(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新权限角色信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限角色信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthRole(AuthRoleBO authRoleBO)
	{
		logger.info("====删除权限角色信息开始====");
		logger.debug("====删除权限角色信息.入参:AuthRoleBO＝[{}]======", authRoleBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthRolePO authRolePO = new AuthRolePO();
		copyAuthRoleBO2AuthRolePO(authRoleBO, authRolePO);
		Boolean deleteResult = authRoleDao.delete(authRolePO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除权限角色信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限角色信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthRole(List<AuthRoleBO> authRoleBOList) throws ConsoleException
	{
		logger.info("====删除权限角色信息列表开始====");
		logger.debug("====删除权限角色信息列表.入参:List<AuthRoleBO>＝[{}]======", authRoleBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthRoleBO itemBO : authRoleBOList)
		{
			ConsoleReturnBO itemResult = removeAuthRole(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除权限角色信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限角色信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countAuthRole(AuthRoleBO authRoleBO)
	{
		logger.info("====统计权限角色信息开始====");
		logger.debug("====统计权限角色信息。入参：AuthRoleBO＝[{}]======", authRoleBO);
		Long returnLong = new Long(0);
		AuthRolePO authRolePO = new AuthRolePO();
		copyAuthRoleBO2AuthRolePO(authRoleBO, authRolePO);
		returnLong = authRoleDao.count(authRolePO);
		logger.debug("====统计权限角色信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计权限角色信息结束====");
		return returnLong;
	}

	private void copyAuthRoleBO2AuthRolePO(AuthRoleBO authRoleBO, AuthRolePO authRolePO)
	{
		beanUtils.copyBeanNotNull(authRoleBO, authRolePO);
		if (authRoleBO.getSysID() != null)
		{
			authRolePO.setSeqID(authRoleBO.getSysID());
		}
	}

	private void copyAuthRolePO2AuthRoleBO(AuthRolePO authRolePO, AuthRoleBO authRoleBO)
	{
		beanUtils.copyBeanNotNull(authRolePO, authRoleBO);
		if (authRolePO.getSeqID() != null)
		{
			authRoleBO.setSysID(authRolePO.getSeqID());
		}
	}

}
